#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
const int N = 1e4 + 10, M = N * 100;
int s[M], p[N];
int nex[N];
int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, m;
		memset(s, 0, sizeof(s));
		memset(p, 0, sizeof(p));
		memset(nex, 0, sizeof(nex));
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= n; i++) {
			scanf("%d", &s[i]);
		}
		for (int i = 1; i <= m; i++) {
			scanf("%d", &p[i]);
		}
		for (int i = 2, j = 0; i <= m; i++) {
			while (j && p[i] != p[j + 1]) {
				j = nex[j];
			}
			if (p[i] == p[j + 1]) {
				j++;
			}
			nex[i] = j;
		}
		int ans = -1;
		for (int i = 1, j = 0; i <= n; i++) {
			while (j && s[i] != p[j + 1]) {
				j = nex[j];
			}
			if (s[i] == p[j + 1]) {
				j++;
			}
			if (j == m) {
				ans = i - m + 1;
				break;
			}
		}
		cout << ans << endl;
	}
	return 0;
}